<?php
class MenuModel {
	public function add($aInfo) {
		$iId = Base::$oBase->Db_insert ( 'menu', $aInfo );
		$this->_setAll ();
		return $iId;
	}
	
	public function getTopArray() {
		return Base::$oBase->Db_getArray ( 'menu', 'id', 'name', array ('parent_id' => 0 ) );
	}
	
	public function get($iId, $sType = 'id') {
		return Base::$oBase->Db_get ( 'menu', array ($sType => $iId ) );
	}
	
	public function edit($aInfo) {
		Base::$oBase->Db_update ( 'menu', $aInfo );
		$this->_setAll ();
		return true;
	}
	
	public function del($iId) {
		return Base::$oBase->Db_del ( 'menu', array ('id' => $iId ) );
	}
	
	public function getList() {
		$aMenu = $this->getAll ();
		return $aMenu ['list'];
	}
	
	public function getAll() {
		$aData = Base::$oBase->Cache_get ( 'menu' );
		if ($aData == '__NOCACHE__') {
			$this->_setAll ();
			$aData = Base::$oBase->Cache_get ( 'menu' );
		}
		return $aData;
	}
	
	private function _setAll() {
		Base::$oBase->Cache_del ( 'menu' );
		$aData = Base::$oBase->Db_ls ( 'menu', array ('parent_id' => 0 ), null, array ('sort' ) );
		$aMenu = array ('list' => array (), 'item' => array () );
		foreach ( $aData ['item'] as $iKey => $aRow ) {
			$aMenu ['list'] [] = $aRow;
			if ($aRow ['action']) {
				if ($aRow ['method']) {
					$sAction = $aRow ['action'] . '::' . $aRow ['method'];
				} else {
					$sAction = $aRow ['action'];
				}
				$aMenu ['item'] [$iKey] = array ('name' => $aRow ['name'], 'action' => $sAction, 'ext' => $aRow ['ext'] );
			} else {
				$aMenu ['item'] [$iKey] = array ('name' => $aRow ['name'] );
			}
			$aSubData = Base::$oBase->Db_ls ( 'menu', array ('parent_id' => $aRow ['id'] ) );
			if ($aSubData ['count']) {
				$aMenu ['list'] = array_merge ( $aMenu ['list'], $aSubData ['item'] );
				foreach ( $aSubData ['item'] as $aSubRow ) {
					if ($aSubRow ['action']) {
						if ($aSubRow ['method']) {
							$sAction = $aSubRow ['action'] . '::' . $aSubRow ['method'];
						} else {
							$sAction = $aSubRow ['action'];
						}
						$aMenu ['item'] [$iKey] [item] [] = array ('name' => $aSubRow ['name'], 'action' => $sAction, 'ext' => $aSubRow ['ext'] );
					} else {
						$aMenu ['item'] [$iKey] [item] [] = array ('name' => $aSubRow ['name'] );
					}
				}
			}
		}
		Base::$oBase->Cache_set ( 'menu', $aMenu, null, 0 );
	}
	
	public function getHtml() {
		$aMenu = $this->getAll ();
		$aItem = $aMenu ['item'];
		$sHtml = '';
		foreach ( $aItem as $aMainItem ) {
			$sSubHtml = '';
			if (is_array ( $aMainItem ['item'] )) {
				foreach ( $aMainItem ['item'] as $aSubItem ) {
					if (Base::$oBase->User_checkAuth_model ( $aSubItem ['action'] )) {
						list ( $sAction, $sMethod ) = explode ( '::', strtolower ( $aSubItem ['action'] ) );
						if ($sAction == '') {
							$sSubHtml .= '';
						} else {
							if ($sMethod) {
								$sUrl = './index.php?a=' . $sAction . '&m=' . $sMethod;
							} else {
								$sUrl = './index.php?a=' . $sAction;
							}
							if ($aSubItem ['ext']) {
								$sUrl .= '&' . $aSubItem ['ext'];
							}
							$sSubHtml .= '<dd nowrap><a href="' . $sUrl . '">' . $aSubItem ['name'] . '</a></dd>';
						}
					}
				}
			}
			if ($sSubHtml != '') {
				$sSubHtml = '<dl class="sub_menu">' . $sSubHtml . '</dl>';
			}
			if (Base::$oBase->User_checkAuth_model ( $aMainItem ['action'] )) {
				list ( $sAction, $sMethod ) = explode ( '::', strtolower ( $aMainItem ['action'] ) );
				if ($sAction) {
					if ($sMethod) {
						$sUrl = './index.php?a=' . $sAction . '&m=' . $sMethod;
					} else {
						$sUrl = './index.php?a=' . $sAction;
					}
					if ($aMainItem ['ext']) {
						$sUrl .= '&' . $aMainItem ['ext'];
					}
					if ($sSubHtml != '') {
						$sHtml .= '<dd><span class="but"><a href="' . $sUrl . '">' . $aMainItem ['name'] . '</a><span>▼</span></span>' . $sSubHtml . '</dd>';
					} else {
						$sHtml .= '<dd><span class="but"><a href="' . $sUrl . '">' . $aMainItem ['name'] . '</a></span>' . $sSubHtml . '</dd>';
					}
				} elseif ($sSubHtml != '') {
					$sHtml .= '<dd><span class="but"><a>' . $aMainItem ['name'] . '</a><span>▼</span></span>' . $sSubHtml . '</dd>';
				}
			} elseif ($sSubHtml != '') {
				$sHtml .= '<dd><span class="but"><a>' . $aMainItem ['name'] . '</a><span>▼</span></span>' . $sSubHtml . '</dd>';
			}
		}
		return '<dl class="menu">' . $sHtml . '</dl>';
	}
}
?>
